#include <algorithm>
#include<iostream>
#include<vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<vector<int>> vv;
        int n = nums.size();
        sort(nums.begin(),nums.end());
        for(int i = 0;i < n;)
        {
            //int target1 = target - nums[i];
            for(int j = i + 1;j < n;)
            {
                long long aim = (long long)target - nums[j] - nums[i];
                int left = j + 1, right = n - 1;
                while(left < right)
                {
                    int sum = nums[left] + nums[right];
                    if(sum < aim)
                    {
                        left++;
                    }
                    else if(sum > aim)
                    {
                        right--;
                    }
                    else
                    {
                        vv.push_back({nums[i],nums[j],nums[left],nums[right]});
                        left++;
                        right--;
                        while(left < right && nums[left - 1] == nums[left]) left++;
                        while(left < right && nums[right + 1] == nums[right]) right--;
                    }
                }
                j++;
                while(j <= n - 3 && nums[j - 1] == nums[j]) j++;
            }
            i++;
            while(i <= n - 4 && nums[i - 1] == nums[i]) i++;
        }
        return vv;
    }
};

int main()
{

	return 0;
}
